A virtual machine (VM) is a collection of services that support a running program at a higher level of abstraction than a physical architecture provides. This may include services such as automatic memory management through garbage collection, dynamic optimization through just-in-time compilation, debugging and instrumentation, and dynamic code loading and reloading. To provide many of these services the VM needs to be able to pause running a program to inspect and modify its state. For example, a stop-the-world garbage collector will at some point need to pause all application threads to update the heap when objects are moved and collected. Conventional architectures and system libraries for threading generally do not provide a native mechanism to pause a running thread, so the VM must provide this itself by adding code to the sequence of instructions. Any added code should have low overhead, since re-using existing synchronization primitives such as locks would mean that application threads would often use these locks even though they are infrequently required. In addition, the paused threads must be paused in a consistent state where modifications to the heap will not conflict with the work of the application threads.
A safepoint is a VM mechanism that allows one thread to pause other threads, with data structures in a consistent state. Existing activity ceases, and new activity proceeds in a consistent state undisturbed by the actions of other threads. The execution of certain actions is guaranteed to not be split across a safepoint, such as reading the address of an object and then writing to that object. If the VM and its programs are in a safepoint, then it is safe to modify shared data structures. Safepoints are used to implement VM services such as stop-the-world garbage collection, which move objects. The VM uses a safepoint to prevent other threads from mutating the heap during such object-moving operations. VM safepoints are restricted to run predefined code in only a subset of threads.